package com.blb.community.handler;

/**
 * @Author 帅帅
 * @Date 2022/7/7 14:52
 * @PackageName:com.blb.community.handler
 * @ClassName: GlobalExceptionsHandler
 * @Description: TODO
 * @Version 1.0
 */

import com.blb.community.utils.ResponseResult;
import com.blb.community.utils.ResponseStatus;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * 全局异常处理
 *
 * @author 帅帅
 */
@Slf4j
@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(ExpiredJwtException.class)
    public ResponseResult<String> jwtExpired(ExpiredJwtException e) {
        return ResponseResult.error(ResponseStatus.JWT_EXPIRED);
    }

    @ExceptionHandler(IOException.class)
    public ResponseResult<String> jwtExpired(IOException e) {
        log.error("io异常",e);
        return ResponseResult.error(ResponseStatus.BUSINESS_ERROR);
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult<String> han(Exception e) {
        log.error("全局异常",e);
        return ResponseResult.error("服务器发生异常");
    }
}
